﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
using AjaxPro;
using System.Data.SqlClient;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.HuanTong
{
    public partial class HTUserGenZong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                BindGridView();
                DropDownListBind(string.Empty);
            }
        }

        #region GridViewBind(x)
        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向   
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];
            // 调用业务数据获取方法   
            DataTable dtBind = this.getDB();
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图   
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            // GridView绑定并显示数据   
            this.GridView1.DataSource = dtBind;
            this.GridView1.DataBind();
        }
        // DataTable 数据源     
        private DataTable getDB()
        {
            DataTable dt = HuanPingService.GetSelectsl();
            foreach (DataRow dr in dt.Rows)
            {
                if (dt.Rows.Count != 0)
                {
                    string dateDiff4 = "";
                    string pj = "";
                    if (dr["xiangmushoulishijians"].ToString() != "")
                    {
                        DateTime DateTime45 = Convert.ToDateTime(dr["xiangmushoulishijians"].ToString());//起始时间
                        DateTime DateTime56 = Convert.ToDateTime(DateTime.Now.Date.ToString("yyyy-MM-dd"));//结束时间                                
                        TimeSpan ts41 = new TimeSpan(DateTime45.Ticks);
                        TimeSpan ts51 = new TimeSpan(DateTime56.Ticks);
                        TimeSpan ts61 = ts41.Subtract(ts51).Duration();
                        int ts44 = HuanPingService.ProNoticeDaysss(DateTime.Now.Date.ToString("yyyy-MM-dd"), dr["xiangmushoulishijians"].ToString(), dr["ProjectName"].ToString());
                        //显示时间
                        dateDiff4 = "用时" + (ts61.Days + 1 - ts44).ToString() + "天";
                        pj = dr["ProjectName"].ToString();
                        //dt.Rows[0]["liuchengshijian"] = dateDiff4;
                        //string td = dt.Rows[0]["liuchengshijian"].ToString();
                        //}
                        if (HuanPingService.Prosj(dateDiff4, pj))
                        {
                        }
                        //else
                        //{
                        //    dateDiff4 = "用时1天";
                        //    dt.Rows[0]["liuchengshijian"] = dateDiff4;
                        //}
                    }
                }
            }
            DataTable dts = HuanPingService.GetSelectsl();
            return dts;
            return dt;
        }
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }
        private void SortGridView(string sortExpression, string direction)
        {
            DataTable dt = getDB();
            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;
            GridView1.DataSource = dv; //将DataView绑定到GridView上
            GridView1.DataBind();
        }
        //处理GridView的排序事件
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, " DESC");
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, " ASC");
            }
        }
        //鼠标滑动
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(GridView1);
        }
        #endregion

        #region DropDownListBind(x)
        public void DropDownListBind(string Deparatment)
        {
            //环评类别
            ddlhuanpingleibie.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("7");
            ddlhuanpingleibie.DataBind();
            ddlhuanpingleibie.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlhuanpingleibie.Items.Insert(1, new ListItem("全部", "全部"));
            //环评单位
            ddlhp.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.GetList();
            ddlhp.DataBind();
            ddlhp.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlhp.Items.Insert(1, new ListItem("全部", "全部"));
            //项目执行部门
            ddlfenpeizhixingbumen.DataSource = EnvironmentManagerSystem.BLL.ren.GetList();
            ddlfenpeizhixingbumen.DataTextField = "username";
            ddlfenpeizhixingbumen.DataValueField = "ID";
            ddlfenpeizhixingbumen.DataBind();
            ddlfenpeizhixingbumen.Items.Insert(0, new ListItem("请选择", "请选择"));
            ddlfenpeizhixingbumen.Items.Insert(1, new ListItem("全部", "全部"));
        }
        #endregion

        #region 按钮(搜索按钮、重置按钮)(x)
        //搜索按钮
        protected void lbtnSelect_Click(object sender, EventArgs e)
        {
            lbtnBind();
        }
        private void lbtnBind()
        {          
            //项目名称,类别,紧急状况,经办人,流程,评价单位
            GridView1.DataSource = NoticeService.GetSelectgz(txtProjectName.Text, ddlhuanpingleibie.SelectedValue, ddljinjizhuangtai.SelectedItem.Text, ddlLiuCheng.SelectedItem.Text, ddlhp.SelectedValue);
            GridView1.DataBind();
        }
        //重置按钮
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            foreach (Control con in this.form1.Controls)
            {
                if (con is TextBox)
                    ((TextBox)con).Text = string.Empty;
                else if (con is CheckBox)
                    ((CheckBox)con).Checked = false;
                else if (con is DropDownList)
                    ((DropDownList)con).SelectedIndex = -1;
            }
        }
        #endregion
    }
}
